<!--
描述：选择题信息组件
开发人：zyanming
开发日期：2018年9月13日
修改人：hqj
修改日期：2018年9月17日
-->
<template lang="pug">
    keep-alive(v-if="pageShow")
        kalix-table(bizKey="choice" title='选择题信息列表'  v-bind:targetURL="targetURL"
        v-bind:bizDialog="bizDialog" v-bind:bizSearch="'ChoiceSearch'" v-bind:btnList="btnList" v-bind:dictDefine="dictDefine"
        v-bind:toolbarBtnList="toolbarBtnList" v-bind:customToolBar="customToolBar" v-bind:customTableTool="customTableTool"
        v-bind:isFixedColumn="isFixedColumn" v-on:afterDialogClose="dialogClose"
        v-bind:jsonStr="jsonStr" v-bind:appendCondition="appendCondition" ref="kalixTable")
            template(slot="tableColumnSlot")
                el-table-column(prop="compareFlag" label="排重比对" align="center" width="120px")
                    template(slot-scope="scope")
                        el-tag(v-if="scope.row.compareFlag === '1'" type="success") 已比对
                        el-tag(v-else type="danger") 未比对
                el-table-column(prop="repeatedFlag" label="排重处理" align="center" width="120px")
                    template(slot-scope="scope")
                        el-tag(v-if="scope.row.repeatedFlag === '1'" type="success") 已处理
                        el-tag(v-else type="danger") 未处理
                el-table-column(prop="checkFlag" label="审核状态" align="center" width="120px")
                    template(slot-scope="scope")
                        template(v-if="scope.row.checkFlag === '1'")
                            el-tag(type="success") 已通过
                        template(v-else-if="scope.row.checkFlag === '2'")
                            el-popover(trigger="hover" placement="top" width="120")
                                | 审核未通过原因
                                br
                                | {{scope.row.checkReason}}
                                div(slot="reference")
                                    el-tag(type="danger") 未通过
                        template(v-else)
                            el-tag 未审核
                <!--el-table-column(prop="delFlag" label="是否删除" align="center" width="120px")-->
                <!--template(slot-scope="scope")-->
                <!--el-tag(v-if="scope.row.delFlag === '1'" type="info") 已删除-->
                <!--el-tag(v-else type="success") 有效-->
                el-table-column(prop="typeName" label="类别" align="center" width="150px")
                el-table-column(prop="stem" label="题干" align="center" width="350px")
                    template(slot-scope="scope")
                        el-popover(trigger="hover" placement="top" width="350")
                            | {{scope.row.stem}}
                            div(slot="reference")
                                template(v-if="scope.row.stem && scope.row.stem.length > 20")
                                    | {{scope.row.stem.substr(0, 20)}}...
                                template(v-else)
                                    | {{scope.row.stem}}
                <!--el-table-column(prop="checker" label="审核员" align="center" width="120px")-->
                <!--el-table-column(prop="checkDate" label="审核时间" align="center" width="180px")-->
                <!--template(slot-scope="scope")-->
                <!--i(class="el-icon-time")-->
                <!--span {{scope.row.checkDate}}-->
                el-table-column(prop="answerA" label="选项A" align="center" width="200px")
                el-table-column(prop="answerB" label="选项B" align="center" width="200px")
                el-table-column(prop="answerC" label="选项C" align="center" width="200px")
                el-table-column(prop="answerD" label="选项D" align="center" width="200px")
                el-table-column(prop="answer" label="正确答案" align="center" width="80px")
                <!--el-table-column(prop="createBy" label="创建者" align="center" width="120px")-->
                <!--el-table-column(prop="creationDate" label="创建时间" align="center" width="180px")-->
                <!--template(slot-scope="scope")-->
                <!--i(class="el-icon-time")-->
                <!--span {{scope.row.creationDate}}-->
                <!--el-table-column(prop="updateBy" label="更新者" align="center" width="120px")-->
                <!--el-table-column(prop="updateDate" label="更新时间" align="center" width="180px")-->
                <!--template(slot-scope="scope")-->
                <!--i(class="el-icon-time")-->
                <!--span {{scope.row.updateDate}}-->
                <!--el-table-column(prop="reason" label="删除原因" align="center" width="200px")-->
                <!--template(slot-scope="scope")-->
                <!--el-popover(trigger="hover" placement="top" width="200")-->
                <!--| {{scope.row.reason}}-->
                <!--div(slot="reference")-->
                <!--template(v-if="scope.row.reason != null && scope.row.reason.length > 10")-->
                <!--| {{scope.row.reason.substr(0, 10)}}...-->
                <!--template(v-else)-->
                <!--| {{scope.row.reason}}-->
    div(v-else) 题库功能已关闭，如需使用请联系管理员！
</template>

<script type="text/ecmascript-6">
    import FormModel from './model'
    import {
        ChoiceTName,
        ChoiceURL,
        ExportExlURL,
        QuestionPreviewURL,
        RepeatedCompareURL,
        TemplatePath
    } from '../../config.toml'
    import {questionInputBtnList1} from '../config.js'
    import {availableQuestion, getServerCurrentDateYear} from '../../common.js'

    export default {
        name: 'kalix-enrolment-choice',
        data() {
            return {
                pageShow: false,
                currentDateYear: (new Date()).getFullYear(),
                formModel: Object.assign({}, FormModel),
                dictDefine: [{
                    cacheKey: 'ENROLMENT-DICT-KEY',
                    type: '类别',
                    targetField: 'typeName',
                    sourceField: 'type'
                }],
                btnList: questionInputBtnList1,
                toolbarBtnList: [
                    {id: 'add', isShow: false},
                    {id: 'newAdd', title: '添加', isShow: true, icon: 'icon-add'},
                    {id: 'importExcel', title: '导入Excel', isShow: true, icon: 'icon-add'},
                    {id: 'exportExcel', title: '导出Excel', isShow: false, icon: 'icon-add'},
                    {id: 'downloadExcel', title: '下载Excel模板', isShow: true, icon: 'icon-add'}
                ],
                targetURL: ChoiceURL,
                bizDialog: [
                    {id: 'view', dialog: 'ChoiceView'},
                    {id: 'newEdit', dialog: 'ChoiceNewAdd'},
                    {id: 'newAdd', dialog: 'ChoiceNewAdd'},
                    {id: 'importExcel', dialog: 'ChoiceImport'},
                    {id: 'exportExcel', dialog: 'ChoiceView'},
                    {id: 'logicDelete', dialog: 'ChoiceDelete'}
                ],
                isFixedColumn: true
            }
        },
        components: {},
        created() {
            availableQuestion(this.axios, this.$KalixMessage, flag => {
                this.pageShow = flag
            })
            getServerCurrentDateYear(this.axios, this.$KalixMessage, flag => {
                this.currentDateYear = flag
            })
        },
        activated() {
        },
        deactivated() {
        },
        computed: {
            jsonStr() {
                return '{"delFlag": "0", "creationDate:begin:gt": "' + this.currentDateYear +
                    '-01-01 00:00:00", "creationDate:end:lt": "' + this.currentDateYear + '-12-31 23:59:59"}'
            },
            appendCondition() {
                return {
                    'delFlag': '0', 'creationDate:begin:gt': this.currentDateYear +
                    '-01-01 00:00:00', 'creationDate:end:lt': this.currentDateYear + '-12-31 23:59:59'
                }
            }
        },
        methods: {
            customToolBar(btnId, table) {
                switch (btnId) {
                    case 'newAdd': {
                        this.onNewAdd(table)
                        break
                    }
                    case 'importExcel': {
                        this.onImportExcel(table)
                        break
                    }
                    case 'exportExcel': {
                        this.onExportExcel(table)
                        break
                    }
                    case 'downloadExcel': {
                        let excelName = ChoiceTName
                        //excelName = encodeURI(excelName)
                        window.open(this.$KalixGlobalConfig.baseURL + TemplatePath
                            + '&filename=' + excelName + '&access_token=' + this.$KalixCatch.get('access_token'))
                        break;
                    }
                }
            },
            onNewAdd(table) {
                //this.table = table
                let dig =
                    table.bizDialog.filter((item) => {
                        return item.id === 'newAdd'
                    })
                table.whichBizDialog = dig[0].dialog
                setTimeout(() => {
                    this.formModel = Object.assign({}, FormModel)
                    table.$refs.kalixDialog.open('添加', false, this.formModel)
                }, 20)
            },
            onImportExcel(table) {
                let dig =
                    table.bizDialog.filter((item) => {
                        return item.id === 'importExcel'
                    })
                table.whichBizDialog = dig[0].dialog
                setTimeout(() => {
                    table.$refs.kalixDialog.$refs.kalixBizDialog.open('批量导入', false, '')
                }, 20)
            },
            onExportExcel(table) {
                const loading = this.$loading({
                    lock: true,
                    text: 'Excel数据导出中...',
                    spinner: 'el-icon-loading',
                    background: 'rgba(255, 255, 255, 0.7)'
                })
                let queryParams = table.getQueryParams()
                this.axios.request({
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    method: 'get',
                    baseURL: '../api',
                    url: ExportExlURL,
                    responseType: 'blob',
                    params: {
                        jsonStr: queryParams,
                        EntityName: 'com.kalix.middleware.excel.api.model.ChoiceDto',
                        ServiceUrl: '/choices/export/list',
                        serviceDictUrl: '/enrolment/dicts/types/values'
                    }
                }).then(response => {
                    if (response.data) {
                        this.download(response.data)
                        this.$KalixMessage.success('导出成功！')
                    } else {
                        this.$KalixMessage.error('导出失败！')
                    }
                    loading.close();
                }).catch((e) => {
                    this.$KalixMessage.error('导出失败！原因：' + e.message)
                    loading.close();
                })
            },
            download(data) {
                let url = window.URL.createObjectURL(new Blob([data]))
                let link = document.createElement('a')
                link.style.display = 'none'
                link.href = url
                link.setAttribute('download', '选择题试题.xlsx')
                document.body.appendChild(link)
                link.click()
            },
            customTableTool(row, btnId, that) {
                switch (btnId) {
                    case 'newEdit': {
                        this.onNewEdit(row, that)
                        break
                    }
                    case 'logicDelete': { // 逻辑删除
                        let warnInfo = '确定要删除吗？'
                        that.$confirm(warnInfo, '警告', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            this.onLogicDelete(row, that)
                        })
                        break
                    }
                    case 'preview': { // 预览
                        window.open(this.$KalixGlobalConfig.baseURL + QuestionPreviewURL
                            + '&beanname=Choice&id=' + row.id + '&access_token=' + this.$KalixCatch.get('access_token'))
                        break
                    }
                    case 'compare': { // 排重比对
                        this.onCompare(row, that)
                        break
                    }
                }
            },
            onNewEdit(row, table) {
                let dig =
                    table.bizDialog.filter((item) => {
                        return item.id === 'newEdit'
                    })
                table.whichBizDialog = dig[0].dialog
                setTimeout(() => {
                    table.$refs.kalixDialog.open('编辑', true, row)
                }, 20)
            },
            onLogicDelete(row, table) {
                let dig =
                    table.bizDialog.filter((item) => {
                        return item.id === 'logicDelete'
                    })
                table.whichBizDialog = dig[0].dialog
                setTimeout(() => {
                    table.$refs.kalixDialog.$refs.kalixBizDialog.open('删除', false, row)
                }, 20)
            },
            onCompare(row, table) {
                let url = RepeatedCompareURL.replace('[beans]', 'choices') + '/' + row.id
                const loading = this.$loading({
                    lock: true,
                    text: '试题排重比对中...',
                    spinner: 'el-icon-loading',
                    background: 'rgba(255, 255, 255, 0.7)'
                })
                this.axios.request({
                    method: 'POST',
                    url: url,
                    data: {'questionId': row.id},
                    params: {}
                }).then(response => {
                    if (response.data.success) {
                        this.$KalixMessage.success(response.data.msg)
                    } else {
                        this.$KalixMessage.error(response.data.msg)
                    }
                    loading.close()
                    // 刷新列表
                    table.refresh()
                }).catch(() => {
                    loading.close()
                    table.refresh()
                })
            },
            dialogClose() {
                this.$refs.kalixTable.refresh()
            }
        }
    }
</script>

<style scoped lang="stylus">
</style>
